package hikari.gui.assets;

import com.gooddies.wiring.annotations.PostWiring;
import com.gooddies.wiring.annotations.Wire;
import com.gooddies.wiring.annotations.WiringComponent;
import hikari.utils.Utils;
import java.awt.Color;
import java.awt.Component;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

/**
 * @author sad
 */
@WiringComponent(singleton = true)
public class AssetsTreeRenderer extends DefaultTreeCellRenderer {

    private ImageIcon textureIcon;
    private ImageIcon model3dIcon;
    private ImageIcon folderIcon;
    private ImageIcon templateIcon;
    @Wire
    private Utils utils;
    @Wire
    private AssetsTree assetsTree;

    private AssetsTreeRenderer() {
    }

    @PostWiring
    private void init() {
        textureIcon = new ImageIcon(utils.loadImageFromResources("icons/texture.png"));
        model3dIcon = new ImageIcon(utils.loadImageFromResources("icons/3dmodel.png"));
        folderIcon = new ImageIcon(utils.loadImageFromResources("icons/folder.png"));
        templateIcon = new ImageIcon(utils.loadImageFromResources("icons/template.png"));
    }

    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
        if (node.getAllowsChildren() == false && node instanceof AssetsNode) {
            AssetsNode assetsNode = (AssetsNode) node;
            if (assetsNode.isConflictNode()) {
                setForeground(Color.RED);
            } else {
                setForeground(Color.BLACK);
            }
        } else {
            setForeground(Color.BLACK);
        }
        if (node.getAllowsChildren()) {
            setIcon(folderIcon);
        } else {
            String fileName = (String) node.getUserObject();
            String fileExtension = utils.getFileExtension(fileName);
            if (assetsTree.isExtension3dModel(fileExtension)) {
                setIcon(model3dIcon);
            } else if (assetsTree.isExtensionTexture(fileExtension)) {
                setIcon(textureIcon);
            } else if (assetsTree.isExtensionTemplate(fileExtension)) {
                setIcon(templateIcon);
            } else {
                setIcon(leafIcon);
            }
        }

        return this;
    }
}
